function Column(id, label, types)
{
	this.id = id;
	this.label = label;
	this.types = types;
}

SearchItem.prototype.columns = new Array();
	
SearchItem.prototype.operators = new Array();

SearchItem.prototype.form_name = "s";

SearchItem.prototype.form = null;

SearchItem.prototype.getNextIndex = function()
{
	var numi = $('#nbSearchArea');
  	var num = (numi.val() - 1) + 2;
  	numi.val(num);
  	return num;
}

function init_select_column_opts(item, select_column)
{
	for (option in SearchItem.columns)
	{
		var opt = document.createElement('option');
		opt.text = SearchItem.columns[option].label;
		opt.value = SearchItem.columns[option].id;
		select_column.appendChild(opt);
	}
  	item.columnChange();
}

function _SearchItemAPI(){
	this.items = new Array();
	this.lang = 'fr';

	this.columnChange = function(item)
	{
		if (SearchItemAPI.items[item.id] !== undefined && SearchItemAPI.items[item.id] !== null)
		{
			SearchItemAPI.items[item.id].columnChange();
		}
	};
	this.deleteItem = function(item){
		if (SearchItemAPI.items[item.id] !== undefined && SearchItemAPI.items[item.id] !== null)
		{
			SearchItemAPI.items[item.id].deleteItem();
		}
	}
}

SearchItemAPI = new _SearchItemAPI();

function SearchItem(search)
{
	this.id = this.getNextIndex();
	this.search = search;
	
	this.search_div = document.createElement('div');
	this.select_column = document.createElement('select');
	this.select_operator = document.createElement('select');
	this.value_item = document.createElement('input');
	this.delete_icon = document.createElement('img');
	
	this.search_div.id = 'search_' + this.id;
	
  	this.select_column.id = 'column_' + this.id;
  	this.select_column.name = SearchItem.form_name + '[columns][' + this.id + ']';
  	
	this.value_item.id = 'value_' + this.id;
  	this.value_item.type='hidden';
  	
  	this.delete_icon.src='images/del.png';
  	this.delete_icon.border='0';
  	this.delete_icon.id='delete_' + this.id;
  	
	this.search_div.appendChild(this.delete_icon);
  	this.search_div.appendChild(this.select_column);
	this.search_div.appendChild(this.select_operator);
	this.search_div.appendChild(this.value_item);
	
	SearchItemAPI.items[this.id] = this;
	SearchItemAPI.items[this.select_column.id] = this;
	SearchItemAPI.items[this.select_operator.id] = this;
	SearchItemAPI.items[this.value_item.id] = this;
	SearchItemAPI.items[this.delete_icon.id] = this;
	
	this.reloadOperators = function(id){
		var type = SearchItem.columns[id].types;
		if (type !== undefined)
		{
			var sel = document.createElement('select');
		  	sel.id = 'operator_' + this.id;
		  	sel.name = SearchItem.form_name + '[operator][' + this.id + ']';
		  	var opt = null;
			if (type instanceof Array){
				{
					opt = document.createElement('option');
					opt.text = SearchItem.operators['='];
					opt.value = '=';
					sel.appendChild(opt);
				}
				{
					opt = document.createElement('option');
					opt.text = SearchItem.operators['!='];
					opt.value = '!=';
					sel.appendChild(opt);
				}
			}else if (type == 'integer'){
				{
					opt = document.createElement('option');
					opt.text = SearchItem.operators['='];
					opt.value = '=';
					sel.appendChild(opt);
				}
				{
					opt = document.createElement('option');
					opt.text = SearchItem.operators['>'];
					opt.value = '>';
					sel.appendChild(opt);
				}
				{
					opt = document.createElement('option');
					opt.text = SearchItem.operators['<'];
					opt.value = '<';
					sel.appendChild(opt);
				}
				{
					opt = document.createElement('option');
					opt.text = SearchItem.operators['!='];
					opt.value = '!=';
					sel.appendChild(opt);
				}
			}else if (type == 'date'){
				{
					opt = document.createElement('option');
					opt.text = SearchItem.operators['='];
					opt.value = '=';
					sel.appendChild(opt);
				}
				{
					opt = document.createElement('option');
					opt.text = SearchItem.operators['>'];
					opt.value = '>';
					sel.appendChild(opt);
				}
				{
					opt = document.createElement('option');
					opt.text = SearchItem.operators['<'];
					opt.value = '<';
					sel.appendChild(opt);
				}
			}else if (type == 'string'){
				{
					opt = document.createElement('option');
					opt.text = SearchItem.operators['like'];
					opt.value = 'like';
					sel.appendChild(opt);
				}
				{
					opt = document.createElement('option');
					opt.text = SearchItem.operators['='];
					opt.value = '=';
					sel.appendChild(opt);
				}
				{
					opt = document.createElement('option');
					opt.text = SearchItem.operators['starts'];
					opt.value = 'starts';
					sel.appendChild(opt);
				}
				{
					opt = document.createElement('option');
					opt.text = SearchItem.operators['ends'];
					opt.value = 'ends';
					sel.appendChild(opt);
				}
				{
					opt = document.createElement('option');
					opt.text = SearchItem.operators['!='];
					opt.value = '!=';
					sel.appendChild(opt);
				}
			}
			
			this.search_div.replaceChild(sel, this.select_operator);
			this.select_operator = sel;
		}
	}
	
	this.reloadInputText = function(id){
		var type = SearchItem.columns[id].types;
		if (type !== undefined)
		{
			var sid = 'value_'+this.id;
			var sname = SearchItem.form_name + '[value]['+this.id+']';
			var oldText = $(this.value_item).val();
			var child = null;
			if (type instanceof Array){
				child = document.createElement('select');
			  	var opt = null;
				for (option in type){
					opt = document.createElement('option');
					opt.text = type[option];
					opt.value = option;
					child.appendChild(opt);
				}
			  	child.id = sid;
			  	child.name = sname;
			}else{
				child = document.createElement('input');
				child.type='text';
			  	child.id = sid;
			  	child.name = sname;
			  	child.value = oldText;
				if (type == 'integer'){
					var mask = new Mask("##", "number");
					mask.attach(child);
				}else if (type == 'date'){
					var d = new Date();
					d = new Date(d.getFullYear()-20, 0, 1);
					$(child).datepicker(
						jQuery.extend(
							{showMonthAfterYear:false,  defaultDate: "-20y" }, 
							$.datepicker.regional[SearchItemAPI.lang]
						)
					);
				}
			}
			
			if (child != null){
				this.search_div.replaceChild(child, this.value_item);
				this.value_item = child;
			}
		}
	}

	this.columnChange = function()
	{
		// search selected option
		if ($(this.select_column).find(":selected").length > 0)
		{
			var id = $(this.select_column).find(":selected").val();
			this.reloadOperators(id);
			this.reloadInputText(id);
		};
	}
	
	this.deleteItem = function() {
		this.search.removeChild(this.search_div);
	}
	
  	init_select_column_opts(this, this.select_column);
  	$('#'+this.select_column.id).live('change', function(){
  		SearchItemAPI.columnChange(this);
  	});
  	
  	$('#'+this.delete_icon.id).live('click', function(){
  		SearchItemAPI.deleteItem(this);
  	});
}

function submit(url){
	SearchItem.form.action = url;
	SearchItem.form.submit();
}
